package com.wanpinghui.supplier.addrbook;

import android.Manifest;
import android.annotation.TargetApi;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.malinskiy.superrecyclerview.SuperRecyclerView;
import com.wanpinghui.supplier.R;
import com.wanpinghui.supplier.addrbook.adapter.AddressBooksAdapter;
import com.wanpinghui.supplier.common.BaseActivity;
import com.wanpinghui.supplier.widgets.TitlePanelLayoutUser;
import com.wanpinghui.wphlibrary.WPHSdk;
import com.wanpinghui.wphlibrary.model.addrbook.AddrbookService;
import com.wanpinghui.wphlibrary.model.addrbook.bean.MobileListBean;
import com.wanpinghui.wphlibrary.model.addrbook.bean.MoblieBean;
import com.yunshl.yunshllibrary.recyclerview.OnItemClickListener;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.List;

import me.nereo.multi_image_selector.MultiImageSelectorFragment;
import retrofit.callback.YRequestCallback;

/**
 * 功能描述
 * author:caitiangui
 * time:2018/1/6
 */
@ContentView(R.layout.layout_activity_add_friends)
public class AddfriendActivity extends BaseActivity {


    @ViewInject(R.id.tpl_title)
    private TitlePanelLayoutUser tpl_title;

    @ViewInject(R.id.tv_add_recording)
    private TextView tv_add_recording;

    @ViewInject(R.id.recyclerview)
    private SuperRecyclerView recyclerView;

    @ViewInject(R.id.tv_null)
    private TextView tv_null;

    private List<MoblieBean> moblieBeanList;

    private AddressBooksAdapter adapter;

    private int REQUEST_SCAN_RESULT = 100;

    @Override
    public String getName() {
        return null;
    }

    @Override
    public boolean isBar() {
        return false;
    }

    @Override
    public void initViews() {
//        ArrayList<HashMap<String, String>> readContact = WPHSdk.getService(AddrbookService.class).readContact(this);
//        List<String> stringList = new ArrayList<>();
//        for (int i = 0; i<readContact.size();i++){
//            stringList.add(readContact.get(i).get("phone"));
//        }
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AddressBooksAdapter(this);
        adapter.setAddFriendsLog(true);
        recyclerView.setAdapter(adapter);
        //   addfriendsLogs();
    }

    @Override
    protected void onResume() {
        super.onResume();
        addfriendsLogs();
    }

    @Override
    public void bindEvents() {
        tpl_title.setOnClickBacktrack(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        findViewById(R.id.tv_add_addrbook_friedns).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(AddfriendActivity.this, AddAddrBookFriendsActivity.class));
            }
        });
        findViewById(R.id.tv_add_scan).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (ContextCompat.checkSelfPermission(AddfriendActivity.this, Manifest.permission.CAMERA)
                        != PackageManager.PERMISSION_GRANTED) {
                    requestPermission(
                            getString(R.string.mis_permission_rationale_write_storage));
                } else
                    startActivityForResult(new Intent(AddfriendActivity.this, ScanActivity.class), REQUEST_SCAN_RESULT);
            }
        });
        adapter.setListener(new AddressBooksAdapter.setOnItemListener() {
            @Override
            public void setAddFriend(int position) {

            }

            @Override
            public void setInvite(int position) {

            }

            @Override
            public void setAgreeFriend(int position) {
                WPHSdk.getService(AddrbookService.class).agreeAddFriendsLogs(adapter.getDatas().get(position).getId_(), new YRequestCallback<MobileListBean>() {
                    @Override
                    public void onSuccess(MobileListBean var1) {
                        addfriendsLogs();
                    }

                    @Override
                    public void onFailed(int var1, String message) {
                        showWarningText(message);

                    }

                    @Override
                    public void onException(Throwable var1) {


                    }
                });

            }
        });

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                if (adapter.getDatas().get(position).getStatus_display().contains("已添加")) {
                    Intent intent = new Intent(AddfriendActivity.this, CustomerInfoActivity.class);
                    intent.putExtra("bean", adapter.getDatas().get(position));
                    startActivity(intent);
                } else {
                    Intent intent = new Intent(AddfriendActivity.this, StrangerInfoActivity.class);
                    intent.putExtra("status_display",adapter.getDatas().get(position).getStatus_display());
                    intent.putExtra("bean", adapter.getDatas().get(position));
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_SCAN_RESULT) {
            if (resultCode == RESULT_OK) {
                showWarningText("等待认证");
            } else {
                if (data != null) {
                    String result = data.getStringExtra("result");
                    if (result != null) {
                        showWarningText(result);
                    }
                }
            }
        }
    }

    @TargetApi(Build.VERSION_CODES.M)
    private void requestPermission(String rationale) {
        if (shouldShowRequestPermissionRationale(Manifest.permission.CAMERA)) {
//            new AlertDialog.Builder(MessageP2PActivity.this)
//                    .setTitle(me.nereo.multi_image_selector.R.string.mis_permission_dialog_title)
//                    .setMessage(rationale)
//                    .setPositiveButton(me.nereo.multi_image_selector.R.string.mis_permission_dialog_ok, new DialogInterface.OnClickListener() {
//                        @Override
//                        public void onClick(DialogInterface dialog, int which) {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, MultiImageSelectorFragment.REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
//                        }
//                    })
//                    .setNegativeButton(me.nereo.multi_image_selector.R.string.mis_permission_dialog_cancel, null)
//                    .create().show();
        } else {
            requestPermissions(new String[]{Manifest.permission.CAMERA}, MultiImageSelectorFragment.REQUEST_STORAGE_WRITE_ACCESS_PERMISSION);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (requestCode == MultiImageSelectorFragment.REQUEST_STORAGE_WRITE_ACCESS_PERMISSION) {
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                startActivity(new Intent(AddfriendActivity.this, ScanActivity.class));
            }
        } else {
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

    private void addfriendsLogs() {
        WPHSdk.getService(AddrbookService.class).addfriendsLogs(new YRequestCallback<MobileListBean>() {
            @Override
            public void onSuccess(MobileListBean var1) {
                moblieBeanList = var1.getList();
                if (moblieBeanList != null && moblieBeanList.size() > 0) {
                    recyclerView.setVisibility(View.VISIBLE);
                    tv_null.setVisibility(View.GONE);
                    tv_add_recording.setVisibility(View.VISIBLE);
                    adapter.setDatas(moblieBeanList);
                    adapter.notifyDataSetChanged();
                } else {
                    tv_null.setVisibility(View.VISIBLE);
                    recyclerView.setVisibility(View.GONE);
                    tv_add_recording.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailed(int var1, String message) {

                //     ll_tv_null.setVisibility(View.VISIBLE);
            }

            @Override
            public void onException(Throwable var1) {
                var1.printStackTrace();
            }
        });

    }

    @Override
    public void initData() {

    }


}
